package sing.core.util

import android.Manifest
import android.app.Activity
import android.app.ActivityManager
import android.content.Context
import android.content.Context.TELEPHONY_SERVICE
import android.content.Intent
import android.content.pm.ApplicationInfo
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.content.pm.ResolveInfo
import android.net.Uri
import android.os.Build
import android.provider.Settings
import android.telephony.TelephonyManager
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import sing.core.base.BaseApp
import java.util.*

object DeviceUtil {

    fun getImei(context: Context): String {
        var imei = ""
        try {
            val tm = context.getSystemService(TELEPHONY_SERVICE) as TelephonyManager
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){
                if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED){
                    return ""
                }
                imei = tm.getDeviceId()
            }else if(Build.VERSION.SDK_INT< 29){
                val method = tm.javaClass.getMethod("getImei")
                imei = method.invoke(tm)?.toString() ?: ""
            }else{
//                imei = Settings.System.getString(context.contentResolver, Settings.Secure.ANDROID_ID)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return imei
    }
}